//
//  LMWebViewController.m
//  WelfareManager
//
//  Created by longmin on 17/4/21.
//  Copyright © 2017年 timtian. All rights reserved.
//

#import "LMWebViewController.h"

#import "YHWebViewProgress.h"
#import "YHWebViewProgressView.h"

@interface LMWebViewController ()<UIWebViewDelegate>

@property (strong, nonatomic) UIWebView *webView;
@property (strong, nonatomic) YHWebViewProgress *progressProxy;
@property (strong, nonatomic) YHWebViewProgressView *progressView;
@property(nonatomic, strong) UIImageView *reloadView;//加载图片

@property(nonatomic, strong) UIView *whiteView;
@end

@implementation LMWebViewController

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.webView];
    [self.view addSubview:self.progressView];
    [self reloadWeb];
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.webView.width = self.view.width;
    self.webView.height = self.view.height - self.navigationBar.bottom;
    self.webView.top = self.navigationBar.bottom;
}

#pragma mark - Event

- (void)reloadWeb {
    [self.whiteView removeFromSuperview];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:self.detailUrl]];
    [self.webView loadRequest:urlRequest];
}

#pragma mark - Public
#pragma mark - Private
#pragma mark - 数据源和代理方法
#pragma mark - UIView -> 下面放继承自UIView的方法
#pragma mark - set方法和get方法

- (UIWebView *)webView {
    if (_webView == nil) {
        _webView = [[UIWebView alloc] init];
        _webView.dataDetectorTypes = UIDataDetectorTypeNone;
        _webView.delegate = self;
        _webView.scrollView.bounces = false;
        _webView.opaque = NO;
        _webView.scalesPageToFit=YES;
        _webView.backgroundColor = [UIColor clearColor];
    }
    return _webView;
}

- (YHWebViewProgressView *)progressView {
    if(_progressView == nil){
        _progressView = [[YHWebViewProgressView alloc] initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), 2)];
        _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleBottomMargin;
    }
    return _progressView;
}

- (YHWebViewProgress *)progressProxy {
    if (_progressProxy == nil) {
        // 创建进度条代理，用于处理进度控制
        _progressProxy = [[YHWebViewProgress alloc] init];
        // 设置进度条
        self.progressProxy.progressView = self.progressView;
        // 将UIWebView代理指向YHWebViewProgress
        self.webView.delegate = self.progressProxy;
        // 设置webview代理转发到self（可选）
        self.progressProxy.webViewProxy = self;
    }
    return _progressProxy;
}

-(UIView *)whiteView {
    if (_whiteView == nil) {
        _whiteView = [[UIView alloc] init];
        self.whiteView.frame = CGRectMake(0, 0, __kScreenWidth, __kScreenHeight);
        self.whiteView.backgroundColor = [UIColor whiteColor];
        [self.whiteView addSubview:self.reloadView];
    }
    return _whiteView;
}

-(UIImageView *)reloadView {
    if (_reloadView == nil) {
        _reloadView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"no_net"]];
        self.reloadView.userInteractionEnabled = YES;
        self.reloadView.centerX = __kScreenWidth * 0.5;
        
        UIControl *reloadControl = [[UIControl alloc] init];
        [reloadControl addTarget:self action:@selector(reloadWeb) forControlEvents:UIControlEventTouchUpInside];
        reloadControl.width = 125;
        reloadControl.height = 35;
        reloadControl.centerX = CGRectGetCenter(self.reloadView.bounds).x;
        reloadControl.bottom = self.reloadView.height - 70;
        [self.reloadView addSubview:reloadControl];
    }
    return _reloadView;
}

- (NSString *)detailUrl {
    if (_detailUrl == nil) {
        _detailUrl=@"http://www.liguanjia.com/index.php/welcome/operation_center";
    }
    return _detailUrl;
}

@end
